package com.fruit.resolver;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

/**
 * 错误信息
 * @author shibeilei
 * @version 2017.11.30 12:20
 */
@ControllerAdvice
public class MyExceptionHandel implements HandlerExceptionResolver {

	private Logger logger = LoggerFactory.getLogger(MyExceptionHandel.class);

	@Override
	@ExceptionHandler
	public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler,
			Exception ex) {

		String returnUrl = "exception/definite";
		System.out.println(request.getRequestURI() + "============" + request.getRequestURL());
		ex.printStackTrace();
		if (ex instanceof DefiniteException) {
			ModelAndView mv = new ModelAndView(returnUrl);
			mv.addObject("message", ex.getMessage());
			this.logError(logger, ex.getMessage());
			return mv;
		} else {
			ModelAndView mv = new ModelAndView(returnUrl);
			mv.addObject("message", "抱歉，您请求的页面没有找到哦！");
			return mv;
		}

	}

	/**
	 * 信息 <br/>
	 * @param logge logger
	 * @param interfaceName interfaceName
	 * @author shibeilei
	 * @version 2017.11.30 11:40
	 */
	public void logBefore(Logger logge, String interfaceName) {
		logge.info("");
		logge.info("start");
		logge.info(interfaceName);
	}

	/**
	 * 信息 <br/>
	 * 
	 * @param logge
	 *            logger
	 * @param interfaceName
	 *            interfaceName
	 * @author shibeilei
	 * @version 2017.11.30 11:40
	 */
	public void logInfo(Logger logge, String interfaceName) {
		logge.info("");
		logge.info("info");
		logge.info(interfaceName);
	}

	/**
	 * 信息 <br/>
	 * 
	 * @param logge
	 *            logger
	 * @param interfaceName
	 *            interfaceName
	 * @author shibeilei
	 * @version 2017.11.30 11:40
	 */
	public void logWarn(Logger logge, String interfaceName) {
		logge.warn("");
		logge.warn("warn");
		logge.warn(interfaceName);
	}

	/**
	 * 信息 <br/>
	 * 
	 * @param logge
	 *            logger
	 * @param interfaceName
	 *            interfaceName
	 * @author shibeilei
	 * @version 2017.11.30 11:40
	 */
	public void logError(Logger logge, String interfaceName) {
		logge.error("");
		logge.error("error");
		logge.error(interfaceName);
	}

	/**
	 * 信息 <br/>
	 * 
	 * @param logge
	 *            logger
	 * @author shibeilei
	 * @version 2017.11.30 11:40
	 */
	public void logAfter(Logger logge) {
		logge.info("end");
		logge.info("");
	}

}
